//
//  MbRewardAd.m
//  MbAdsSdk
//
//  Created by 吴奶强 on 2021/12/17.
//

#import "MbDrawAd.h"
#import <BUAdSDK/BUAdSDK.h>
#import <KSAdSDK/KSAdSDK.h>
#import <ABUADSDK/ABUADSDK.h>



@interface MbDrawAd()<BUNativeExpressAdViewDelegate,KSDrawAdsManagerDelegate>

@property(nonatomic,strong)BUNativeExpressAdManager *ttDrawAd;
@property(nonatomic,strong)KSDrawAdsManager *ksDrawAd;

@property(nonatomic,assign)NSInteger index;

@property(nonatomic,strong)NSError *error;

@end

@implementation MbDrawAd


-(void)loadDraw{
    if(!self.adItems || self.adItems.count == self.index  || !self.rootVC || !self.view){
        if(!self.error){
            self.error = [[NSError alloc]initWithDomain:@"配置信息错误" code:0 userInfo:nil];
        }
        if ([self.delegate respondsToSelector:@selector(drawAdDidError:)]) {
            [self.delegate drawAdDidError:self.error];
        }
        return;
    }
    AdItem *adItem = [self.adItems objectAtIndex:self.index];
    if(adItem.plat == Pangolin){
        BUAdSlot *slot1 = [[BUAdSlot alloc] init];
        slot1.ID = adItem.codeNo;
        slot1.AdType = BUAdSlotAdTypeDrawVideo; //required
        slot1.isOriginAd = YES; //required
        slot1.position = BUAdSlotPositionTop;
        slot1.imgSize = [BUSize sizeBy:BUProposalSize_DrawFullScreen];
        if (!self.ttDrawAd) {
           self.ttDrawAd = [[BUNativeExpressAdManager alloc] initWithSlot:slot1 adSize:self.view.bounds.size];
        }
        self.ttDrawAd.adSize = self.view.bounds.size;
        self.ttDrawAd.delegate = self;
        [self.ttDrawAd loadAdDataWithCount:1];
        
    } else if(adItem.plat == GDT){
        
       
        
    } else if(adItem.plat == KS){
        self.ksDrawAd = [[KSDrawAdsManager alloc] initWithPosId:adItem.codeNo];
        self.ksDrawAd.delegate = self;
        [self.ksDrawAd loadAdDataWithCount:1];
    } else if(adItem.plat == GroMore){
       
    }
    self.index ++;
}


#pragma mark 穿山甲回调
- (void)nativeExpressAdSuccessToLoad:(BUNativeExpressAdManager *)nativeExpressAdManager views:(NSArray<__kindof BUNativeExpressAdView *> *)views{
    if (views.count) {
        [views enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            BUNativeExpressAdView *expressView = (BUNativeExpressAdView *)obj;
            expressView.rootViewController = self.rootVC;
            // important: 此处会进行WKWebview的渲染，建议一次最多预加载三个广告，如果超过3个会很大概率导致WKWebview渲染失败。
            [expressView render];
        }];
    }
}

- (void)nativeExpressAdViewRenderSuccess:(BUNativeExpressAdView *)nativeExpressAdView{
    [self.view addSubview:nativeExpressAdView];
}
- (void)nativeExpressAdFailToLoad:(BUNativeExpressAdManager *)nativeExpressAdManager error:(NSError *)error{
    self.error = error;
    [self loadDraw];
}

#pragma mark 优量汇回调

#pragma mark 快手回调

- (void)drawAdsManagerSuccessToLoad:(KSDrawAdsManager *)adsManager drawAds:(NSArray<KSDrawAd *> *)drawAdDataArray{
    KSDrawAd *drawAd = drawAdDataArray[0];
    [drawAd registerContainer:self.view];
    //[self.view addSubview:drawAd.v];
}

- (void)drawAdsManager:(KSDrawAdsManager *)adsManager didFailWithError:(NSError *)error{
    self.error = error;
    [self loadDraw];
}

#pragma mark gromore回调



@end
